<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Class template weighted_mean</title>
<link rel="stylesheet" href="../../../../../../../doc/src/boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../../../index.html" title="Chapter 1. Boost.Histogram">
<link rel="up" href="../../../histogram/reference.html#header.boost.histogram.accumulators.weighted_mean_hpp" title="Header &lt;boost/histogram/accumulators/weighted_mean.hpp&gt;">
<link rel="prev" href="thread_safe.html" title="Class template thread_safe">
<link rel="next" href="weighted_sum.html" title="Class template weighted_sum">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../../boost.png"></td>
<td align="center"><a href="../../../../../../../index.html">Home</a></td>
<td align="center"><a href="../../../../../../libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../../../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="thread_safe.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../../../histogram/reference.html#header.boost.histogram.accumulators.weighted_mean_hpp"><img src="../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../index.html"><img src="../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="weighted_sum.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="refentry">
<a name="boost.histogram.accumulators.weighted_mean"></a><div class="titlepage"></div>
<div class="refnamediv">
<h2><span class="refentrytitle">Class template weighted_mean</span></h2>
<p>boost::histogram::accumulators::weighted_mean — Calculates mean and variance of weighted sample. </p>
</div>
<h2 xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" class="refsynopsisdiv-title">Synopsis</h2>
<div xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" class="refsynopsisdiv"><pre class="synopsis"><span class="comment">// In header: &lt;<a class="link" href="../../../histogram/reference.html#header.boost.histogram.accumulators.weighted_mean_hpp" title="Header &lt;boost/histogram/accumulators/weighted_mean.hpp&gt;">boost/histogram/accumulators/weighted_mean.hpp</a>&gt;

</span><span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">typename</span> ValueType<span class="special">&gt;</span> 
<span class="keyword">class</span> <a class="link" href="weighted_mean.html" title="Class template weighted_mean">weighted_mean</a> <span class="special">{</span>
<span class="keyword">public</span><span class="special">:</span>
  <span class="comment">// types</span>
  <span class="keyword">typedef</span> <span class="identifier">ValueType</span>          <a name="boost.histogram.accumulators.weighted_mean.value_type"></a><span class="identifier">value_type</span><span class="special">;</span>     
  <span class="keyword">typedef</span> <span class="keyword">const</span> <span class="identifier">value_type</span> <span class="special">&amp;</span> <a name="boost.histogram.accumulators.weighted_mean.const_reference"></a><span class="identifier">const_reference</span><span class="special">;</span>

  <span class="comment">// <a class="link" href="weighted_mean.html#boost.histogram.accumulators.weighted_meanconstruct-copy-destruct">construct/copy/destruct</a></span>
  <a class="link" href="weighted_mean.html#idm13471-bb"><span class="identifier">weighted_mean</span></a><span class="special">(</span><span class="special">)</span> <span class="special">=</span> <span class="keyword">default</span><span class="special">;</span>
  <span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">typename</span> T<span class="special">&gt;</span> <a class="link" href="weighted_mean.html#idm13472-bb"><span class="identifier">weighted_mean</span></a><span class="special">(</span><span class="keyword">const</span> <a class="link" href="weighted_mean.html" title="Class template weighted_mean">weighted_mean</a><span class="special">&lt;</span> <span class="identifier">T</span> <span class="special">&gt;</span> <span class="special">&amp;</span><span class="special">)</span><span class="special">;</span>
  <a class="link" href="weighted_mean.html#idm13479-bb"><span class="identifier">weighted_mean</span></a><span class="special">(</span><span class="identifier">const_reference</span><span class="special">,</span> <span class="identifier">const_reference</span><span class="special">,</span> <span class="identifier">const_reference</span><span class="special">,</span> 
                <span class="identifier">const_reference</span><span class="special">)</span><span class="special">;</span>

  <span class="comment">// <a class="link" href="weighted_mean.html#idm13402-bb">public member functions</a></span>
  <span class="keyword">void</span> <a class="link" href="weighted_mean.html#idm13403-bb"><span class="keyword">operator</span><span class="special">(</span><span class="special">)</span></a><span class="special">(</span><span class="identifier">const_reference</span><span class="special">)</span><span class="special">;</span>
  <span class="keyword">void</span> <a class="link" href="weighted_mean.html#idm13408-bb"><span class="keyword">operator</span><span class="special">(</span><span class="special">)</span></a><span class="special">(</span><span class="keyword">const</span> <a class="link" href="../weight_type.html" title="Struct template weight_type">weight_type</a><span class="special">&lt;</span> <span class="identifier">value_type</span> <span class="special">&gt;</span> <span class="special">&amp;</span><span class="special">,</span> <span class="identifier">const_reference</span><span class="special">)</span><span class="special">;</span>
  <a class="link" href="weighted_mean.html" title="Class template weighted_mean">weighted_mean</a> <span class="special">&amp;</span> <a class="link" href="weighted_mean.html#idm13416-bb"><span class="keyword">operator</span><span class="special">+=</span></a><span class="special">(</span><span class="keyword">const</span> <a class="link" href="weighted_mean.html" title="Class template weighted_mean">weighted_mean</a> <span class="special">&amp;</span><span class="special">)</span><span class="special">;</span>
  <a class="link" href="weighted_mean.html" title="Class template weighted_mean">weighted_mean</a> <span class="special">&amp;</span> <a class="link" href="weighted_mean.html#idm13424-bb"><span class="keyword">operator</span> <span class="special">*=</span></a><span class="special">(</span><span class="identifier">const_reference</span><span class="special">)</span> <span class="keyword">noexcept</span><span class="special">;</span>
  <span class="keyword">bool</span> <a class="link" href="weighted_mean.html#idm13432-bb"><span class="keyword">operator</span><span class="special">==</span></a><span class="special">(</span><span class="keyword">const</span> <a class="link" href="weighted_mean.html" title="Class template weighted_mean">weighted_mean</a> <span class="special">&amp;</span><span class="special">)</span> <span class="keyword">const</span> <span class="keyword">noexcept</span><span class="special">;</span>
  <span class="keyword">bool</span> <a class="link" href="weighted_mean.html#idm13437-bb"><span class="keyword">operator</span><span class="special">!=</span></a><span class="special">(</span><span class="keyword">const</span> <a class="link" href="weighted_mean.html" title="Class template weighted_mean">weighted_mean</a> <span class="special">&amp;</span><span class="special">)</span> <span class="keyword">const</span> <span class="keyword">noexcept</span><span class="special">;</span>
  <span class="identifier">const_reference</span> <a class="link" href="weighted_mean.html#idm13442-bb"><span class="identifier">sum_of_weights</span></a><span class="special">(</span><span class="special">)</span> <span class="keyword">const</span> <span class="keyword">noexcept</span><span class="special">;</span>
  <span class="identifier">const_reference</span> <a class="link" href="weighted_mean.html#idm13445-bb"><span class="identifier">sum_of_weights_squared</span></a><span class="special">(</span><span class="special">)</span> <span class="keyword">const</span> <span class="keyword">noexcept</span><span class="special">;</span>
  <span class="identifier">value_type</span> <a class="link" href="weighted_mean.html#idm13448-bb"><span class="identifier">count</span></a><span class="special">(</span><span class="special">)</span> <span class="keyword">const</span> <span class="keyword">noexcept</span><span class="special">;</span>
  <span class="identifier">const_reference</span> <a class="link" href="weighted_mean.html#idm13453-bb"><span class="identifier">value</span></a><span class="special">(</span><span class="special">)</span> <span class="keyword">const</span> <span class="keyword">noexcept</span><span class="special">;</span>
  <span class="identifier">value_type</span> <a class="link" href="weighted_mean.html#idm13458-bb"><span class="identifier">variance</span></a><span class="special">(</span><span class="special">)</span> <span class="keyword">const</span><span class="special">;</span>
  <span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">typename</span> Archive<span class="special">&gt;</span> <span class="keyword">void</span> <a class="link" href="weighted_mean.html#idm13463-bb"><span class="identifier">serialize</span></a><span class="special">(</span><span class="identifier">Archive</span> <span class="special">&amp;</span><span class="special">,</span> <span class="keyword">unsigned</span><span class="special">)</span><span class="special">;</span>
<span class="special">}</span><span class="special">;</span></pre></div>
<div class="refsect1">
<a name="idm21606"></a><h2>Description</h2>
<p>Uses West's incremental algorithm to improve numerical stability of mean and variance computation. </p>
<div class="refsect2">
<a name="idm21609"></a><h3>
<a name="boost.histogram.accumulators.weighted_meanconstruct-copy-destruct"></a><code class="computeroutput">weighted_mean</code> 
        public
       construct/copy/destruct</h3>
<div class="orderedlist"><ol class="orderedlist" type="1">
<li class="listitem"><pre class="literallayout"><a name="idm13471-bb"></a><span class="identifier">weighted_mean</span><span class="special">(</span><span class="special">)</span> <span class="special">=</span> <span class="keyword">default</span><span class="special">;</span></pre></li>
<li class="listitem">
<pre class="literallayout"><span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">typename</span> T<span class="special">&gt;</span> <a name="idm13472-bb"></a><span class="identifier">weighted_mean</span><span class="special">(</span><span class="keyword">const</span> <a class="link" href="weighted_mean.html" title="Class template weighted_mean">weighted_mean</a><span class="special">&lt;</span> <span class="identifier">T</span> <span class="special">&gt;</span> <span class="special">&amp;</span> o<span class="special">)</span><span class="special">;</span></pre>Allow implicit conversion from other weighted_means. </li>
<li class="listitem">
<pre class="literallayout"><a name="idm13479-bb"></a><span class="identifier">weighted_mean</span><span class="special">(</span><span class="identifier">const_reference</span> wsum<span class="special">,</span> <span class="identifier">const_reference</span> wsum2<span class="special">,</span> 
              <span class="identifier">const_reference</span> mean<span class="special">,</span> <span class="identifier">const_reference</span> variance<span class="special">)</span><span class="special">;</span></pre>Initialize to external sum of weights, sum of weights squared, mean, and variance. </li>
</ol></div>
</div>
<div class="refsect2">
<a name="idm21657"></a><h3>
<a name="idm13402-bb"></a><code class="computeroutput">weighted_mean</code> public member functions</h3>
<div class="orderedlist"><ol class="orderedlist" type="1">
<li class="listitem">
<pre class="literallayout"><span class="keyword">void</span> <a name="idm13403-bb"></a><span class="keyword">operator</span><span class="special">(</span><span class="special">)</span><span class="special">(</span><span class="identifier">const_reference</span> x<span class="special">)</span><span class="special">;</span></pre>Insert sample x. </li>
<li class="listitem">
<pre class="literallayout"><span class="keyword">void</span> <a name="idm13408-bb"></a><span class="keyword">operator</span><span class="special">(</span><span class="special">)</span><span class="special">(</span><span class="keyword">const</span> <a class="link" href="../weight_type.html" title="Struct template weight_type">weight_type</a><span class="special">&lt;</span> <span class="identifier">value_type</span> <span class="special">&gt;</span> <span class="special">&amp;</span> w<span class="special">,</span> <span class="identifier">const_reference</span> x<span class="special">)</span><span class="special">;</span></pre>Insert sample x with weight w. </li>
<li class="listitem">
<pre class="literallayout"><a class="link" href="weighted_mean.html" title="Class template weighted_mean">weighted_mean</a> <span class="special">&amp;</span> <a name="idm13416-bb"></a><span class="keyword">operator</span><span class="special">+=</span><span class="special">(</span><span class="keyword">const</span> <a class="link" href="weighted_mean.html" title="Class template weighted_mean">weighted_mean</a> <span class="special">&amp;</span> rhs<span class="special">)</span><span class="special">;</span></pre>Add another <code class="computeroutput"><a class="link" href="weighted_mean.html" title="Class template weighted_mean">weighted_mean</a></code>.</li>
<li class="listitem">
<pre class="literallayout"><a class="link" href="weighted_mean.html" title="Class template weighted_mean">weighted_mean</a> <span class="special">&amp;</span> <a name="idm13424-bb"></a><span class="keyword">operator</span> <span class="special">*=</span><span class="special">(</span><span class="identifier">const_reference</span> s<span class="special">)</span> <span class="keyword">noexcept</span><span class="special">;</span></pre>Scale by value. <p>This acts as if all samples were scaled by the value. </p>
</li>
<li class="listitem"><pre class="literallayout"><span class="keyword">bool</span> <a name="idm13432-bb"></a><span class="keyword">operator</span><span class="special">==</span><span class="special">(</span><span class="keyword">const</span> <a class="link" href="weighted_mean.html" title="Class template weighted_mean">weighted_mean</a> <span class="special">&amp;</span> rhs<span class="special">)</span> <span class="keyword">const</span> <span class="keyword">noexcept</span><span class="special">;</span></pre></li>
<li class="listitem"><pre class="literallayout"><span class="keyword">bool</span> <a name="idm13437-bb"></a><span class="keyword">operator</span><span class="special">!=</span><span class="special">(</span><span class="keyword">const</span> <a class="link" href="weighted_mean.html" title="Class template weighted_mean">weighted_mean</a> <span class="special">&amp;</span> rhs<span class="special">)</span> <span class="keyword">const</span> <span class="keyword">noexcept</span><span class="special">;</span></pre></li>
<li class="listitem">
<pre class="literallayout"><span class="identifier">const_reference</span> <a name="idm13442-bb"></a><span class="identifier">sum_of_weights</span><span class="special">(</span><span class="special">)</span> <span class="keyword">const</span> <span class="keyword">noexcept</span><span class="special">;</span></pre>Return sum of weights. </li>
<li class="listitem">
<pre class="literallayout"><span class="identifier">const_reference</span> <a name="idm13445-bb"></a><span class="identifier">sum_of_weights_squared</span><span class="special">(</span><span class="special">)</span> <span class="keyword">const</span> <span class="keyword">noexcept</span><span class="special">;</span></pre>Return sum of weights squared (variance of weight distribution). </li>
<li class="listitem">
<pre class="literallayout"><span class="identifier">value_type</span> <a name="idm13448-bb"></a><span class="identifier">count</span><span class="special">(</span><span class="special">)</span> <span class="keyword">const</span> <span class="keyword">noexcept</span><span class="special">;</span></pre>Return effective counts. <p>This corresponds to the equivalent number of unweighted samples that would have the same variance as this sample. count() should be used to check whether value() and variance() are defined, see documentation of value() and variance(). count() can be used to compute the variance of the mean by dividing variance() by count(). </p>
</li>
<li class="listitem">
<pre class="literallayout"><span class="identifier">const_reference</span> <a name="idm13453-bb"></a><span class="identifier">value</span><span class="special">(</span><span class="special">)</span> <span class="keyword">const</span> <span class="keyword">noexcept</span><span class="special">;</span></pre>Return mean value of accumulated weighted samples. <p>The result is undefined, if count() == 0. </p>
</li>
<li class="listitem">
<pre class="literallayout"><span class="identifier">value_type</span> <a name="idm13458-bb"></a><span class="identifier">variance</span><span class="special">(</span><span class="special">)</span> <span class="keyword">const</span><span class="special">;</span></pre>Return variance of accumulated weighted samples. <p>The result is undefined, if count() == 0 or count() == 1. </p>
</li>
<li class="listitem"><pre class="literallayout"><span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">typename</span> Archive<span class="special">&gt;</span> <span class="keyword">void</span> <a name="idm13463-bb"></a><span class="identifier">serialize</span><span class="special">(</span><span class="identifier">Archive</span> <span class="special">&amp;</span> ar<span class="special">,</span> <span class="keyword">unsigned</span><span class="special">)</span><span class="special">;</span></pre></li>
</ol></div>
</div>
</div>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright © 2016-2019 Hans
      Dembinski<p>
        Distributed under the Boost Software License, Version 1.0. (See accompanying
        file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
      </p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="thread_safe.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../../../histogram/reference.html#header.boost.histogram.accumulators.weighted_mean_hpp"><img src="../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../index.html"><img src="../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="weighted_sum.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>
